<?php



require_once './include/common.inc.php';
require_once './include/email.class.php';
checkLogin();

$page_title = 'jans2en furniture - container - Confirmation';
$baner_img='<div class="bigtitle">订单完成</div>';
$postiondiv='<div class="position"><a href="index.php">首页</a> &gt; <span class="position">订单完成</span></div>';


$info_ary = $JANSEN_CONTAINER_ARY;
$total_qty = $JANSEN_CONTAINER_TOTAL_QTY;
$total_qty2 = $JANSEN_CONTAINER_TOTAL_QTY2;
$total_cost = $JANSEN_CONTAINER_TOTAL_COST;
$total_cost1 = $JANSEN_CONTAINER_TOTAL_COST1;
$total_cbm = $JANSEN_CONTAINER_TOTAL_CBM;
$total_cuft = $JANSEN_CONTAINER_TOTAL_CUFT;

/*foreach($JANSEN_CONTAINER_ARY as $v)
{ 
echo $k;
foreach($v as $v2)
{
  echo $v2;
  }

}*/
//echo "<br/>cart_submit_cost:".$total_cost;

if (empty($info_ary)) {
   // header("Location:cart_view.php");
    //exit();
}



$all_product_info_ary = array();
$sql_product_info_ary = array();
foreach ($info_ary as $pid=>$vv) {
    @list($type, $pid,$fitemid) = explode("_", $pid);
    $pid_ary[] = $pid;
	$sql_product_info_ary[] = $type . '|' . $pid . '|' . $vv['count'] . '|' . $vv['price'] . '|' . $vv['name']. '|' . $vv['fitemid'];
}

$from="CN";

if (!empty($sql_product_info_ary)) {
    $sql_product_info_ary = implode("{}", $sql_product_info_ary);
    $sql_product_info_ary = addslashes($sql_product_info_ary);
    $sql = "INSERT INTO `order` (`product_info`, `totalcount`, `totalcost`, `user_id`, `date`,`from`) VALUES ('$sql_product_info_ary', '$total_qty', '$total_cost', '$jansen_uid', '$timestamp','$from')";
    $db->query($sql);
    $orderid = $db->insert_id();
}

// չﳵ
$cart->emptyAll();
saveCart($db, $jansen_uid, "");

// ʱû=ʽû
if ($jansen_status == 1) {
    $sql = "UPDATE `user` SET `status` = '2' WHERE `id`='" . $jansen_uid . "' AND `status` = 1";
    $db->query($sql);
}



if (!empty($pid_ary)) {
    $pid_str = implode(",", $pid_ary);
    $sql = "SELECT `id`,`namezh`,`code`, `small_pic` FROM `product` WHERE `id` IN (" . $pid_str . ")";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $all_product_info_ary[$rs['id']] = $rs;
    }
}

if ($jansen_type == 1) {
    $money_type = " HKD";
} else {
    $money_type = " 美金";
}

$sql = "SELECT * FROM `user` WHERE `id`='$jansen_uid'";
$user_info_ary = $db->get_one($sql);



$sql = "SELECT * FROM `inventory`";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    $location_info_ary[$rs['id']] = $rs['name'];
}

//Send Email
$dealer_name = $user_info_ary['firstname'];
$dealer_email = $user_info_ary['email'];


$system_ary = getSystemValue("");
$webmaster_name = $system_ary['webmaster_namezh'];
$webmaster_email = $system_ary['webmaster_emailzh'];

//require_once './mail/order_to_dealer.php';
//echo $content;exit;
//sendhtmlmail($from_name,$from_email,$to_name,$to_email,$subject,$content);


require_once './mail/order_to_webmaster.php';
//echo $content;exit;
//sendhtmlmail($from_name,$from_email,$to_name,$to_email,$subject,$content);

//echo $content;
//exit;

// excelĵ

$filename = 'attachments/order_' . $jansen_uid . '.xls';
$data = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
<!--
@page
	{margin:1.0in .75in 1.0in .75in;
	mso-header-margin:.5in;
	mso-footer-margin:.5in;}
br
	{mso-data-placement:same-cell;}
.style0
	{mso-number-format:General;
	text-align:general;
	vertical-align:bottom;
	white-space:nowrap;
	mso-rotate:0;
	mso-background-source:auto;
	mso-pattern:auto;
	color:windowtext;
	font-size:12.0pt;
	font-weight:400;
	font-style:normal;
	text-decoration:none;
	mso-font-charset:136;
	mso-protection:locked visible;
	mso-style-id:0;}
td
	{mso-style-parent:style0;
	padding-top:1px;
	padding-right:1px;
	padding-left:1px;
	mso-ignore:padding;
	color:windowtext;
	font-size:12.0pt;
	font-weight:400;
	font-style:normal;
	text-decoration:none;
	mso-font-charset:136;
	text-align:general;
	vertical-align:bottom;
	mso-background-source:auto;
	mso-pattern:auto;
	mso-protection:locked visible;
	white-space:nowrap;
	mso-rotate:0;}
.xl24
	{mso-style-parent:style0;
	font-size:8.0pt;
	font-family:Arial, sans-serif;
	mso-font-charset:0;}
.xl25
	{mso-style-parent:style0;
	font-size:8.0pt;
	font-weight:700;
	font-family:Arial, sans-serif;
	mso-font-charset:0;}
.xl26
	{mso-style-parent:style0;
	font-weight:700;}
ruby
	{ruby-align:left;}
rt
	{color:windowtext;
	font-size:9.0pt;
	font-weight:400;
	font-style:normal;
	text-decoration:none;
	mso-font-charset:136;
	mso-char-type:none;
	display:none;}
-->
</style>
</head>

<body>
<table width="100%" border="1" cellspacing="0" cellpadding="5">
<tr>
<td class=xl25>Item No.</td>
<td class=xl25>Sections</td>
<td class=xl25>Finish</td>
<td class=xl25>Description</td>
<td class=xl25>Qty</td>
<td class=xl25>Price(' . $money_type . ')</td>
<td class=xl25>Total(' . $money_type . ')</td>
<td class=xl25>Order date</td>
</tr>';

//$data = "Item No.\tSections\tQty\tPrice($money_type)\tOrder date\n";


foreach ($info_ary as $k=>$v) {
    @list($type, $k,$fitemid) = explode("_", $k);
    $location = $v['intype'] == 0 ? 'Products' : $location_info_ary[$v['intype']];

     $vname=trim($all_product_info_ary[$k]['namezh'])==""?$v['name']:$all_product_info_ary[$k]['namezh'];
    $data .= "<tr>
					  <td class=xl24>" . $all_product_info_ary[$k]['code'] . "</td> 
					  <td class=xl24>" . $location . "</td> 
					  <td class=xl24>" . $_finish_item[$v['fitemid']] . "</td> 
					  <td class=xl24>" . $vname . "</td> 
					  <td class=xl24>" . $v['count'] . "</td> 
					  <td class=xl24>" . $v['price'] . "</td> 
					  <td class=xl24>" . $v['cost'] . "</td> 
					  <td class=xl24>" . date('d/M/Y') . "</td></tr>";
}

$data .= "
</table>
</body>
</html>";

if(count($info_ary)>0)
{
writeover($filename,$data,"rb+",1,1,0);
$attachment = fread(fopen($filename, "r"), filesize($filename));
$mail = new mime_mail();
$mail->from = "$from_name <$from_email>";
//$mail->headers = "To: $to_name";


	 $to_name ="=?utf-8?B?".base64_encode($to_name)."?=";
if (strstr($to_email, ",")) {
    $tmp_ary = explode(",", $to_email);
    foreach ($tmp_ary as $v) 
	{ 	  
	  // $to_name = "=?utf-8?B?".base64_encode($to_name)."?=";
	   
	   $tmp_ary2[]= "$to_name <$v>";
    }  
	$to = implode(",", $tmp_ary2);
	

} else
 {
   // $to_name ="=?utf-8?B?".base64_encode($to_name)."?=";
    $to   = "$to_name <$to_email>";
}

$mail->to = $to;
$mail->subject = $subject;
$mail->body = $content;
$mail->add_attachment("$attachment", "order.xls", "application/vnd.ms-excel");
$mail->send();
@unlink($filename);

}
//sendhtmlmailCN($from_name,$from_email,$to_name,$to_email,$subject,$content,$attachment);

require_once PrintEot('header');
require_once PrintEot('cart_submit');
require_once PrintEot('footer');

?>